<?php

namespace app\controller\backed;

use app\BaseController;
use app\lib\JwtHander;
use app\model\Admin as AdminModel;
use think\facade\Cache;
use app\lib\exception\NotFoundException;
use app\lib\exception\VerifyException;
use think\facade\Config;

class Passport extends BaseController
{


    /**
     * 登陆接口
     */
    public function login()
    {
        $params = request()->param();

        $data = [
            'username' => $params['username'],
            'password' => $params['password']
        ];

        $admin = AdminModel::getInfoByUsername($data['username']);

        if(!$admin){
            throw new NotFoundException();
        }

        $encryPassword = encryPassword($data['password'],Config::get('base.salt'));
        if ($encryPassword == $admin['password']) {
            $jwt = JwtHander::getInstance()->generateJwt($admin['id'], request()->host());
            return $this->successMessage('登陆成功',$jwt);
        } else {
            throw new VerifyException();
        }

    }


    /**
     * 获取管理员信息
     */
    public function info()
    {
        
        $jwtInfo = JwtHander::getInstance()->decodeJwt();

        $adminModel = new AdminModel();
        $admin = $adminModel->find(1);
        
        return $this->successMessage('查询成功',$admin);

    }

}